import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Profile from '../views/Profile.vue'  // 新增
import DeviceManagement from '../views/DeviceManagement.vue'  // 新增

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: { requiresAuth: true }
    },
    {  // 新增个人中心路由
        path: '/profile',
        name: 'Profile',
        component: Profile,
        meta: { requiresAuth: true }
    },
    {  // 新增设备管理路由
        path: '/devices',
        name: 'DeviceManagement',
        component: DeviceManagement,
        meta: { requiresAuth: true }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth) {
        const userInfo = localStorage.getItem('userInfo')
        if (userInfo) {
            next()
        } else {
            next('/login')
        }
    } else {
        next()
    }
})

export default router